#!/usr/bin/python3
import sys
import struct
import math
from myerror import *

DEGTORAD=math.pi/180.0
RADTODEG=180.0/math.pi
SCLUNIT=1.525878906250000e-05
ERROR_END=100
 
class HisdHeader:
    '''
        这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！
    '''
    def __init__(self,file) -> None:
        self.fp=file
        #bytes
        self.basic_info={}
        self.data_info={}
        self.proj_info={}
        self.navi_info={}
        self.calib_info={}
        self.intercalib_info={}
        self.segm_info={}
        self.navicorr_info={}
        self.obstime={}
        self.error_info={}
        self.spare={}
        #实际类型
        self.correcttable={}
    def read(self):
        '''
        这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！
        '''
        self.correcttable["flag"]=0
        if self.fp==None:
            return ERROR_READ
        #1--------------------------------------------------------------------
        try:
            self.basic_info["HeaderNum"]=self.fp.read(1)
            self.basic_info["BlockLen"]=self.fp.read(2)
            self.basic_info["headerNum"]=self.fp.read(2)
            self.basic_info["byteOrder"]=self.fp.read(1)
            self.basic_info["satName"]=self.fp.read(16)
            self.basic_info["proNum"]=self.fp.read(16)
            self.basic_info["ObsType1"]=self.fp.read(4)
            self.basic_info["ObsType2"]=self.fp.read(2)
            self.basic_info["TimeLine"]=self.fp.read(2)
            self.basic_info["ObsStartTime"]=self.fp.read(8)
            self.basic_info["ObsEndTime"]=self.fp.read(8)
            self.basic_info["fileCreationMjd"]=self.fp.read(8)
            self.basic_info["totalHeaderLen"]=self.fp.read(4)
            self.basic_info["dataLen"]=self.fp.read(4)
            self.basic_info["qflag1"]=self.fp.read(1)
            self.basic_info["qflag2"]=self.fp.read(1)
            self.basic_info["qflag3"]=self.fp.read(1)
            self.basic_info["qflag4"]=self.fp.read(1)
            self.basic_info["verName"]=self.fp.read(32)
            self.basic_info["fileName"]=self.fp.read(128)
            self.basic_info["spare"]=self.fp.read(40)
        except Exception:
            sys.stderr.write("header #1 read error\n")
            return ERROR_READ
        byteFlag=0
        if(int.from_bytes(self.basic_info["BlockLen"],sys.byteorder)!=282):
            byteFlag=1
            self.basic_info["BlockLen"]=bytes(reversed(self.basic_info["BlockLen"]))
            self.basic_info["headerNum"]=bytes(reversed(self.basic_info["headerNum"]))
            self.basic_info["ObsStartTime"]=bytes(reversed(self.basic_info["ObsStartTime"]))
            self.basic_info["ObsEndTime"]=bytes(reversed(self.basic_info["ObsEndTime"]))
            self.basic_info["TimeLine"]=bytes(reversed(self.basic_info["TimeLine"]))
            self.basic_info["fileCreationMjd"]=bytes(reversed(self.basic_info["fileCreationMjd"]))
            self.basic_info["totalHeaderLen"]=bytes(reversed(self.basic_info["totalHeaderLen"]))
            self.basic_info["dataLen"]=bytes(reversed(self.basic_info["dataLen"]))
        if int.from_bytes(self.basic_info["HeaderNum"],sys.byteorder)!=1 or int.from_bytes(self.basic_info["BlockLen"],sys.byteorder)!=282:
            sys.stderr.write("header #1 read error\n")
            HeaderNum_Err="HeaderNum="+str(int.from_bytes(self.basic_info["HeaderNum"],sys.byteorder))+"\n"
            sys.stderr.write(HeaderNum_Err)
            BlockLen_Err="BlockLen="+str(int.from_bytes(self.basic_info["BlockLen"],sys.byteorder))+"\n"
            sys.stderr.write(BlockLen_Err)
            return ERROR_READ
        #2--------------------------------------------------------------------
        #再次提醒！这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！

        try:
            self.data_info["HeaderNum"]=self.fp.read(1)
            self.data_info["BlockLen"]=self.fp.read(2)
            self.data_info["bitPix"]=self.fp.read(2)
            self.data_info["nPix"]=self.fp.read(2)
            self.data_info["nLin"]=self.fp.read(2)
            self.data_info["comp"]=self.fp.read(1)
            self.data_info["spare"]=self.fp.read(40)
        except Exception:
            sys.stderr.write("header #2 read error\n")
            return ERROR_READ
        if byteFlag==1:
            self.data_info["BlockLen"]=bytes(reversed(self.data_info["BlockLen"]))
            self.data_info["bitPix"]=bytes(reversed(self.data_info["bitPix"]))
            self.data_info["nPix"]=bytes(reversed(self.data_info["nPix"]))
            self.data_info["nLin"]=bytes(reversed(self.data_info["nLin"]))
        if int.from_bytes(self.data_info["HeaderNum"],sys.byteorder)!=2 or int.from_bytes(self.data_info["BlockLen"],sys.byteorder)!=50:
            sys.stderr.write("header #2 read error\n")
            HeaderNum_Err="HeaderNum="+str(int.from_bytes(self.basic_info["HeaderNum"],sys.byteorder))+"\n"
            sys.stderr.write(HeaderNum_Err)
            BlockLen_Err="BlockLen="+str(int.from_bytes(self.basic_info["BlockLen"],sys.byteorder))+"\n"
            sys.stderr.write(BlockLen_Err) 
            return ERROR_READ

        #3--------------------------------------------------------------------
        #再次提醒！这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！
        try:
            self.proj_info["HeaderNum"]=self.fp.read(1)
            self.proj_info["BlockLen"]=self.fp.read(2)
            self.proj_info["subLon"]=self.fp.read(8)
            self.proj_info["cfac"]=self.fp.read(4)
            self.proj_info["lfac"]=self.fp.read(4)
            self.proj_info["coff"]=self.fp.read(4)
            self.proj_info["loff"]=self.fp.read(4)
            self.proj_info["satDis"]=self.fp.read(8)
            self.proj_info["eqtrRadius"]=self.fp.read(8)
            self.proj_info["polrRadius"]=self.fp.read(8)
            self.proj_info["projParam1"]=self.fp.read(8)
            self.proj_info["projParam2"]=self.fp.read(8)
            self.proj_info["projParam3"]=self.fp.read(8)
            self.proj_info["projParamSd"]=self.fp.read(8)
            self.proj_info["resampleKind"]=self.fp.read(2)
            self.proj_info["resampleSize"]=self.fp.read(2)
            self.proj_info["spare"]=self.fp.read(40)
        except Exception:
            sys.stderr.write("header #3 read error\n")
            return ERROR_READ
        if byteFlag==1:
            self.proj_info["BlockLen"]=bytes(reversed(self.proj_info["BlockLen"]))
            self.proj_info["subLon"]=bytes(reversed(self.proj_info["subLon"]))
            self.proj_info["cfac"]=bytes(reversed(self.proj_info["cfac"]))
            self.proj_info["lfac"]=bytes(reversed(self.proj_info["lfac"]))
            self.proj_info["coff"]=bytes(reversed(self.proj_info["coff"]))
            self.proj_info["loff"]=bytes(reversed(self.proj_info["loff"]))
            self.proj_info["satDis"]=bytes(reversed(self.proj_info["satDis"]))
            self.proj_info["eqtrRadius"]=bytes(reversed(self.proj_info["eqtrRadius"]))
            self.proj_info["polrRadius"]=bytes(reversed(self.proj_info["polrRadius"]))
            self.proj_info["projParam1"]=bytes(reversed(self.proj_info["projParam1"]))
            self.proj_info["projParam2"]=bytes(reversed(self.proj_info["projParam2"]))
            self.proj_info["projParam3"]=bytes(reversed(self.proj_info["projParam3"]))
            self.proj_info["projParamSd"]=bytes(reversed(self.proj_info["projParamSd"]))
            self.proj_info["resampleKind"]=bytes(reversed(self.proj_info["resampleKind"]))
            self.proj_info["resampleSize"]=bytes(reversed(self.proj_info["resampleSize"]))
        if int.from_bytes(self.proj_info["HeaderNum"],sys.byteorder)!=3 or int.from_bytes(self.proj_info["BlockLen"],sys.byteorder)!=127:
            sys.stderr.write("header #3 read error\n")
            HeaderNum_Err="HeaderNum="+str(int.from_bytes(self.proj_info["HeaderNum"],sys.byteorder))+"\n"
            sys.stderr.write(HeaderNum_Err)
            BlockLen_Err="BlockLen="+str(int.from_bytes(self.proj_info["BlockLen"],sys.byteorder))+"\n"
            sys.stderr.write(BlockLen_Err)
            return ERROR_READ
        #4--------------------------------------------------------------------
        #再次提醒！这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！
        try:
            self.navi_info["HeaderNum"]=self.fp.read(1)
            self.navi_info["BlockLen"]=self.fp.read(2)
            self.navi_info["navMjd"]=self.fp.read(8)
            self.navi_info["sspLon"]=self.fp.read(8)
            self.navi_info["sspLat"]=self.fp.read(8)
            self.navi_info["satDis"]=self.fp.read(8)
            self.navi_info["nadirLon"]=self.fp.read(8)
            self.navi_info["nadirLat"]=self.fp.read(8)
            self.navi_info["sunPos_x"]=self.fp.read(8)
            self.navi_info["sunPos_y"]=self.fp.read(8)
            self.navi_info["sunPos_z"]=self.fp.read(8)
            self.navi_info["moonPos_x"]=self.fp.read(8)
            self.navi_info["moonPos_y"]=self.fp.read(8)
            self.navi_info["moonPos_z"]=self.fp.read(8)
            self.navi_info["spare"]=self.fp.read(40)
        except Exception:
            sys.stderr.write("header #4 read error\n")
            return ERROR_READ
        if byteFlag==1:
            self.navi_info["BlockLen"]=bytes(reversed(self.navi_info["BlockLen"]))
            self.navi_info["navMjd"]=bytes(reversed(self.navi_info["navMjd"]))
            self.navi_info["sspLon"]=bytes(reversed(self.navi_info["sspLon"]))
            self.navi_info["sspLat"]=bytes(reversed(self.navi_info["sspLat"]))
            self.navi_info["satDis"]=bytes(reversed(self.navi_info["satDis"]))
            self.navi_info["nadirLon"]=bytes(reversed(self.navi_info["nadirLon"]))
            self.navi_info["nadirLat"]=bytes(reversed(self.navi_info["nadirLat"]))
            self.navi_info["sunPos_x"]=bytes(reversed(self.navi_info["eqtrRsunPos_xadius"]))
            self.navi_info["sunPos_y"]=bytes(reversed(self.navi_info["sunPos_y"]))
            self.navi_info["sunPos_z"]=bytes(reversed(self.navi_info["sunPos_z"]))
            self.navi_info["moonPos_x"]=bytes(reversed(self.navi_info["moonPos_x"]))
            self.navi_info["moonPos_y"]=bytes(reversed(self.navi_info["moonPos_y"]))
            self.navi_info["moonPos_z"]=bytes(reversed(self.navi_info["moonPos_z"]))
        if int.from_bytes(self.navi_info["HeaderNum"],sys.byteorder)!=4 or int.from_bytes(self.navi_info["BlockLen"],sys.byteorder)!=139:
            sys.stderr.write("header #4 read error\n")
            HeaderNum_Err="HeaderNum="+str(int.from_bytes(self.navi_info["HeaderNum"],sys.byteorder))+"\n"
            sys.stderr.write(HeaderNum_Err)
            BlockLen_Err="BlockLen="+str(int.from_bytes(self.navi_info["BlockLen"],sys.byteorder))+"\n"
            sys.stderr.write(BlockLen_Err)
            return ERROR_READ
        #5--------------------------------------------------------------------
        #再次提醒！这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！
        try:
            self.calib_info["HeaderNum"]=self.fp.read(1)
            self.calib_info["BlockLen"]=self.fp.read(2)
            self.calib_info["bandNo"]=self.fp.read(2)
            self.calib_info["waveLen"]=self.fp.read(8)
            self.calib_info["bitPix"]=self.fp.read(2)
            self.calib_info["errorCount"]=self.fp.read(2)
            self.calib_info["outCount"]=self.fp.read(2)
            self.calib_info["gain_cnt2rad"]=self.fp.read(8)
            self.calib_info["cnst_cnt2rad"]=self.fp.read(8)
        except Exception:
            sys.stderr.write("header #5 read error\n")
            return ERROR_READ
        if byteFlag==1:
            self.calib_info["BlockLen"]=bytes(reversed(self.calib_info["BlockLen"]))
            self.calib_info["bandNo"]=bytes(reversed(self.calib_info["bandNo"]))
            self.calib_info["waveLen"]=bytes(reversed(self.calib_info["waveLen"]))
            self.calib_info["bitPix"]=bytes(reversed(self.calib_info["bitPix"]))
            self.calib_info["errorCount"]=bytes(reversed(self.calib_info["errorCount"]))
            self.calib_info["outCount"]=bytes(reversed(self.calib_info["outCount"]))
            self.calib_info["gain_cnt2rad"]=bytes(reversed(self.calib_info["gain_cnt2rad"]))
            self.calib_info["cnst_cnt2rad"]=bytes(reversed(self.calib_info["cnst_cnt2rad"]))
        if (int.from_bytes(self.calib_info["bandNo"],sys.byteorder)>=7 and self.basic_info["satName"].decode().find("Himawari")!=-1) or (int.from_bytes(self.calib_info["bandNo"],sys.byteorder)>=2 and self.basic_info["satName"].decode().find("MTSAT-2")!=-1):
            try:
                self.calib_info["rad2btp_c0"]=self.fp.read(8)
                self.calib_info["rad2btp_c1"]=self.fp.read(8)
                self.calib_info["rad2btp_c2"]=self.fp.read(8)
                self.calib_info["btp2rad_c0"]=self.fp.read(8)
                self.calib_info["btp2rad_c1"]=self.fp.read(8)
                self.calib_info["btp2rad_c2"]=self.fp.read(8)
                self.calib_info["lightSpeed"]=self.fp.read(8)
                self.calib_info["planckConst"]=self.fp.read(8)
                self.calib_info["bolzConst"]=self.fp.read(8)
                self.calib_info["spare"]=self.fp.read(40)
            except Exception:
                sys.stderr.write("header #5 read error\n")
                return ERROR_READ
            if byteFlag==1:
                self.calib_info["rad2btp_c0"]=bytes(reversed(self.calib_info["rad2btp_c0"]))
                self.calib_info["rad2btp_c1"]=bytes(reversed(self.calib_info["rad2btp_c1"]))
                self.calib_info["rad2btp_c2"]=bytes(reversed(self.calib_info["rad2btp_c2"]))
                self.calib_info["btp2rad_c0"]=bytes(reversed(self.calib_info["btp2rad_c0"]))
                self.calib_info["btp2rad_c1"]=bytes(reversed(self.calib_info["btp2rad_c1"]))
                self.calib_info["btp2rad_c2"]=bytes(reversed(self.calib_info["btp2rad_c2"]))
                self.calib_info["lightSpeed"]=bytes(reversed(self.calib_info["lightSpeed"]))   
                self.calib_info["planckConst"]=bytes(reversed(self.calib_info["planckConst"]))
                self.calib_info["bolzConst"]=bytes(reversed(self.calib_info["bolzConst"]))    
        else:
            try:
                self.calib_info["rad2albedo"]=self.fp.read(8)
                self.calib_info["spareV"]=self.fp.read(104)
            except Exception:
                sys.stderr.write("header #5 read error\n")
                return ERROR_READ
            if byteFlag==1:
                self.calib_info["rad2albedo"]=bytes(reversed(self.calib_info["rad2albedo"]))
        #print(self.fp.tell())
        if int.from_bytes(self.calib_info["HeaderNum"],sys.byteorder)!=5 or int.from_bytes(self.calib_info["BlockLen"],sys.byteorder)!=147:
            #print(self.fp.tell())
            sys.stderr.write("header #5 read error\n")
            HeaderNum_Err="HeaderNum="+str(int.from_bytes(self.calib_info["HeaderNum"],sys.byteorder))+"\n"
            sys.stderr.write(HeaderNum_Err)
            BlockLen_Err="BlockLen="+str(int.from_bytes(self.calib_info["BlockLen"],sys.byteorder))+"\n"
            sys.stderr.write(BlockLen_Err)
            return ERROR_READ    
        #6--------------------------------------------------------------------
        #再次提醒！这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！
        version=float(self.basic_info["verName"].decode()[0:3])
        self.intercalib_info["gsicsCorr_C"]=INVALID_VALUE
        self.intercalib_info["gsicsCorr_1"]=INVALID_VALUE
        self.intercalib_info["gsicsCorr_2"]=INVALID_VALUE
        self.intercalib_info["gsicsBias"]=INVALID_VALUE
        self.intercalib_info["gsicsUncert"]=INVALID_VALUE
        self.intercalib_info["gsicsStscene"]=INVALID_VALUE
        self.intercalib_info["gsicsCorr_StrMJD"]=INVALID_VALUE
        self.intercalib_info["gsicsCorr_EndMJD"]=INVALID_VALUE
        self.intercalib_info["gsicsUpperLimit"]=INVALID_VALUE
        self.intercalib_info["gsicsLowerLimit"]=INVALID_VALUE
        self.intercalib_info["gsicsCorr_C_er"]=INVALID_VALUE
        self.intercalib_info["gsicsCorr_1_er"]=INVALID_VALUE
        self.intercalib_info["gsicsCorr_2_er"]=INVALID_VALUE
        if version>1.1:
            #for version 1.2
            try:
                self.intercalib_info["HeaderNum"]=self.fp.read(1)
                #print(self.fp.tell())
                self.intercalib_info["BlockLen"]=self.fp.read(2)
                self.intercalib_info["gsicsCorr_C"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_1"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_2"]=self.fp.read(8)
                self.intercalib_info["gsicsBias"]=self.fp.read(8)
                self.intercalib_info["gsicsUncert"]=self.fp.read(8)
                self.intercalib_info["gsicsStscene"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_StrMJD"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_EndMJD"]=self.fp.read(8)
                self.intercalib_info["gsicsUpperLimit"]=self.fp.read(4)
                self.intercalib_info["gsicsLowerLimit"]=self.fp.read(4)
                self.intercalib_info["gsicsFilename"]=self.fp.read(128)
                self.intercalib_info["spare"]=self.fp.read(56)
            except Exception:
                sys.stderr.write("header #6 read error\n")
                return ERROR_READ
            if byteFlag==1:
                self.intercalib_info["BlockLen"]=bytes(reversed(self.intercalib_info["BlockLen"]))
                self.intercalib_info["gsicsCorr_2"]=bytes(reversed(self.intercalib_info["gsicsCorr_2"]))
                self.intercalib_info["gsicsCorr_1"]=bytes(reversed(self.intercalib_info["gsicsCorr_1"]))
                self.intercalib_info["gsicsCorr_C"]=bytes(reversed(self.intercalib_info["gsicsCorr_C"]))
                self.intercalib_info["gsicsBias"]=bytes(reversed(self.intercalib_info["gsicsBias"]))
                self.intercalib_info["gsicsUncert"]=bytes(reversed(self.intercalib_info["gsicsUncert"]))
                self.intercalib_info["gsicsStscene"]=bytes(reversed(self.intercalib_info["gsicsStscene"]))   
                self.intercalib_info["gsicsCorr_StrMJD"]=bytes(reversed(self.intercalib_info["gsicsCorr_StrMJD"]))
                self.intercalib_info["gsicsCorr_EndMJD"]=bytes(reversed(self.intercalib_info["gsicsCorr_EndMJD"]))
                self.intercalib_info["gsicsUpperLimit"]=bytes(reversed(self.intercalib_info["gsicsUpperLimit"]))
                self.intercalib_info["gsicsLowerLimit"]=bytes(reversed(self.intercalib_info["gsicsLowerLimit"]))
        elif version>1.0:
             #for version 1.1
            try:
                self.intercalib_info["HeaderNum"]=self.fp.read(1)
                self.intercalib_info["BlockLen"]=self.fp.read(2)
                self.intercalib_info["gsicsCorr_C"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_C_er"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_1"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_1_er"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_2"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_2_er"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_StrMJD"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_EndMJD"]=self.fp.read(8)
                self.intercalib_info["gsicsUpperLimit"]=self.fp.read(4)
                self.intercalib_info["gsicsLowerLimit"]=self.fp.read(4)
                self.intercalib_info["gsicsFilename"]=self.fp.read(128)
                self.intercalib_info["spare"]=self.fp.read(56)
            except Exception:
                sys.stderr.write("header #6 read error\n")
                return ERROR_READ
            if byteFlag==1:
                self.intercalib_info["BlockLen"]=bytes(reversed(self.intercalib_info["BlockLen"]))
                self.intercalib_info["gsicsCorr_2"]=bytes(reversed(self.intercalib_info["gsicsCorr_2"]))
                self.intercalib_info["gsicsCorr_2_er"]=bytes(reversed(self.intercalib_info["gsicsCorr_2_er"]))
                self.intercalib_info["gsicsCorr_1"]=bytes(reversed(self.intercalib_info["gsicsCorr_1"]))
                self.intercalib_info["gsicsCorr_1_er"]=bytes(reversed(self.intercalib_info["gsicsCorr_1_er"]))
                self.intercalib_info["gsicsCorr_C"]=bytes(reversed(self.intercalib_info["gsicsCorr_C"]))
                self.intercalib_info["gsicsCorr_C_er"]=bytes(reversed(self.intercalib_info["gsicsCorr_C_er"]))
                self.intercalib_info["gsicsCorr_StrMJD"]=bytes(reversed(self.intercalib_info["gsicsCorr_StrMJD"]))
                self.intercalib_info["gsicsCorr_EndMJD"]=bytes(reversed(self.intercalib_info["gsicsCorr_EndMJD"]))
                self.intercalib_info["gsicsUpperLimit"]=bytes(reversed(self.intercalib_info["gsicsCorr_StrMJD"]))
                self.intercalib_info["gsicsLowerLimit"]=bytes(reversed(self.intercalib_info["gsicsCorr_EndMJD"]))
        else:
            #for version 1.0 and 0.0
            try:
                self.intercalib_info["HeaderNum"]=self.fp.read(1)
                self.intercalib_info["BlockLen"]=self.fp.read(2)
                self.intercalib_info["gsicsCorr_C"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_C_er"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_1"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_1_er"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_2"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_2_er"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_StrMJD"]=self.fp.read(8)
                self.intercalib_info["gsicsCorr_EndMJD"]=self.fp.read(8)
                self.intercalib_info["gsicsCorrInfo"]=self.fp.read(64)
                self.intercalib_info["spare"]=self.fp.read(128)
            except Exception:
                sys.stderr.write("header #6 read error\n")
                return ERROR_READ
            if byteFlag==1:
                self.intercalib_info["BlockLen"]=bytes(reversed(self.intercalib_info["BlockLen"]))
                self.intercalib_info["gsicsCorr_2"]=bytes(reversed(self.intercalib_info["gsicsCorr_2"]))
                self.intercalib_info["gsicsCorr_2_er"]=bytes(reversed(self.intercalib_info["gsicsCorr_2_er"]))
                self.intercalib_info["gsicsCorr_1"]=bytes(reversed(self.intercalib_info["gsicsCorr_1"]))
                self.intercalib_info["gsicsCorr_1_er"]=bytes(reversed(self.intercalib_info["gsicsCorr_1_er"]))
                self.intercalib_info["gsicsCorr_C"]=bytes(reversed(self.intercalib_info["gsicsCorr_C"]))
                self.intercalib_info["gsicsCorr_C_er"]=bytes(reversed(self.intercalib_info["gsicsCorr_C_er"]))
                self.intercalib_info["gsicsCorr_StrMJD"]=bytes(reversed(self.intercalib_info["gsicsCorr_StrMJD"]))
                self.intercalib_info["gsicsCorr_EndMJD"]=bytes(reversed(self.intercalib_info["gsicsCorr_EndMJD"]))
        if int.from_bytes(self.intercalib_info["HeaderNum"],sys.byteorder)!=6 or int.from_bytes(self.intercalib_info["BlockLen"],sys.byteorder)!=259:
            sys.stderr.write("header #6 read error\n")
            HeaderNum_Err="HeaderNum="+str(int.from_bytes(self.intercalib_info["HeaderNum"],sys.byteorder))+"\n"
            sys.stderr.write(HeaderNum_Err)
            BlockLen_Err="BlockLen="+str(int.from_bytes(self.intercalib_info["BlockLen"],sys.byteorder))+"\n"
            sys.stderr.write(BlockLen_Err) 
            return ERROR_READ
            

        #7--------------------------------------------------------------------
        #再次提醒！这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！
        try:
            self.segm_info["HeaderNum"]=self.fp.read(1)
            #print(self.fp.tell())
            self.segm_info["BlockLen"]=self.fp.read(2)
            self.segm_info["totalSegNum"]=self.fp.read(1)
            self.segm_info["segSeqNo"]=self.fp.read(1)
            #print(self.fp.tell())
            self.segm_info["strLineNo"]=self.fp.read(2)
            self.segm_info["spare"]=self.fp.read(40)
        except Exception:
            sys.stderr.write("header #8 read error\n")
            return ERROR_READ
        if byteFlag==1:
            self.segm_info["BlockLen"]=bytes(reversed(self.navicorr_info["BlockLen"]))
            self.navicorr_info["strLineNo"]=bytes(reversed(self.navicorr_info["BlockLen"]))
        if int.from_bytes(self.segm_info["HeaderNum"],sys.byteorder)!=7 or int.from_bytes(self.segm_info["BlockLen"],sys.byteorder)!=47:
            sys.stderr.write("header #7 read error\n")
            HeaderNum_Err="HeaderNum="+str(int.from_bytes(self.navi_info["HeaderNum"],sys.byteorder))+"\n"
            sys.stderr.write(HeaderNum_Err)
            BlockLen_Err="BlockLen="+str(int.from_bytes(self.navi_info["BlockLen"],sys.byteorder))+"\n"
            sys.stderr.write(BlockLen_Err)
            return ERROR_READ    
        #8--------------------------------------------------------------------
        #再次提醒！这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！
        try:
            self.navicorr_info["HeaderNum"]=self.fp.read(1)
            self.navicorr_info["BlockLen"]=self.fp.read(2)
            self.navicorr_info["RoCenterColumn"]=self.fp.read(4)
            self.navicorr_info["RoCenterLine"]=self.fp.read(4)
            self.navicorr_info["RoCorrection"]=self.fp.read(8)
            self.navicorr_info["correctNum"]=self.fp.read(2)
        except Exception:
            sys.stderr.write("header #8 read error\n")
            return ERROR_READ
        if byteFlag==1:
            self.navicorr_info["BlockLen"]=bytes(reversed(self.navicorr_info["BlockLen"]))
            self.navicorr_info["RoCenterColumn"]=bytes(reversed(self.navicorr_info["RoCenterColumn"]))
            self.navicorr_info["RoCenterLine"]=bytes(reversed(self.navicorr_info["RoCenterLine"]))
            self.navicorr_info["RoCorrection"]=bytes(reversed(self.navicorr_info["RoCorrection"]))
            self.navicorr_info["correctNum"]=bytes(reversed(self.navicorr_info["correctNum"]))
        self.navicorr_info["lineNo"]=[]
        self.navicorr_info["columnShift"]=[]
        self.navicorr_info["lineShift"]=[]
        #print(int.from_bytes(self.navicorr_info["correctNum"],sys.byteorder))
        for i in range(int.from_bytes(self.navicorr_info["correctNum"],sys.byteorder)):
            try:
                self.navicorr_info["lineNo"].append(self.fp.read(2))
                self.navicorr_info["columnShift"].append(self.fp.read(4))
                self.navicorr_info["lineShift"].append(self.fp.read(4))
            except Exception:
                sys.stderr.write("header #8 read error\n")
                return ERROR_READ
        try:
            self.navicorr_info["spare"]=self.fp.read(40)
        except Exception:
            sys.stderr.write("header #8 read error\n")
            return ERROR_READ
        if byteFlag==1:
            for i in range(int.from_bytes(self.navicorr_info["correctNum"],sys.byteorder)):
                self.navicorr_info["lineNo"][i]=bytes(reversed(self.navicorr_info["lineNo"][i]))
                self.navicorr_info["columnShift"][i]=bytes(reversed(self.navicorr_info["columnShift"][i]))
                self.navicorr_info["lineShift"][i]=bytes(reversed(self.navicorr_info["lineShift"][i]))
        if int.from_bytes(self.navicorr_info["HeaderNum"],sys.byteorder)!=8:
            sys.stderr.write("header #8 read error\n")
            HeaderNum_Err="HeaderNum="+str(int.from_bytes(self.navicorr_info["HeaderNum"],sys.byteorder))+"\n"
            sys.stderr.write(HeaderNum_Err)
            BlockLen_Err="BlockLen="+str(int.from_bytes(self.navicorr_info["BlockLen"],sys.byteorder))+"\n"
            sys.stderr.write(BlockLen_Err)
            return ERROR_READ

        #9--------------------------------------------------------------------
        #再次提醒！这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！
        try:
            self.obstime["HeaderNum"]=self.fp.read(1)
            self.obstime["BlockLen"]=self.fp.read(2)
            self.obstime["obsNum"]=self.fp.read(2)
        except Exception:
            sys.stderr.write("header #9 read error\n")
            return ERROR_READ
        if byteFlag==1:
            self.obstime["BlockLen"]=bytes(reversed(self.obstime["BlockLen"]))
            self.obstime["obsNum"]=bytes(reversed(self.obstime["obsNum"]))
        self.obstime["lineNo"]=[]
        self.obstime["obsMJD"]=[]
        for i in range(int.from_bytes(self.obstime["obsNum"],sys.byteorder)):
            try:
                self.obstime["lineNo"].append(self.fp.read(2))
                self.obstime["obsMJD"].append(self.fp.read(8))
            except Exception:
                sys.stderr.write("header #9 read error\n")
                return ERROR_READ
        try:
            self.obstime["spare"]=self.fp.read(40)
        except Exception:
            sys.stderr.write("header #9 read error\n")
            return ERROR_READ
        if byteFlag==1:
            for i in range(int.from_bytes(self.obstime["obsNum"])):
                self.obstime["lineNo"][i]=bytes(reversed(self.obstime["lineNo"][i]))
                self.obstime["obsMJD"][i]=bytes(reversed(self.obstime["obsMJD"][i]))
        if int.from_bytes(self.obstime["HeaderNum"],sys.byteorder)!=9:
            sys.stderr.write("header #9 read error\n")
            HeaderNum_Err="HeaderNum="+str(int.from_bytes(self.navi_info["HeaderNum"],sys.byteorder))+"\n"
            sys.stderr.write(HeaderNum_Err)
            BlockLen_Err="BlockLen="+str(int.from_bytes(self.navi_info["BlockLen"],sys.byteorder))+"\n"
            sys.stderr.write(BlockLen_Err)
            return ERROR_READ 
        #10-------------------------------------------------------------------   
        #再次提醒！这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！
        try:
            self.error_info["HeaderNum"]=self.fp.read(1)
            self.error_info["BlockLen"]=self.fp.read(4)
            self.error_info["errorNum"]=self.fp.read(2)
        except Exception:
            sys.stderr.write("header #10 read error\n")
            return ERROR_READ
        if byteFlag==1:
            self.error_info["BlockLen"]=bytes(reversed(self.error_info["BlockLen"]))
            self.error_info["errorNum"]=bytes(reversed(self.error_info["errorNum"]))
        if int.from_bytes(self.error_info["errorNum"],sys.byteorder)>=1:
            self.error_info["lineNo"]=[]
            self.error_info["errPixNum"]=[]
        for i in range(int.from_bytes(self.error_info["errorNum"],sys.byteorder)):
            try:
                self.error_info["lineNo"].append(self.fp.read(2))
                self.error_info["errPixNum"].append(self.fp.read(2))
            except Exception:
                sys.stderr.write("header #9 read error\n")
                return ERROR_READ
        try:
            self.error_info["spare"]=self.fp.read(40)
        except Exception:
            sys.stderr.write("header #10 read error\n")
            return ERROR_READ
        if byteFlag==1:
            self.error_info["lineNo"]=bytes(reversed(self.error_info["lineNo"]))
            self.error_info["errPixNum"]=bytes(reversed(self.error_info["errPixNum"]))
        if int.from_bytes(self.error_info["HeaderNum"],sys.byteorder)!=10:
            sys.stderr.write("header #10 read error\n")
            HeaderNum_Err="HeaderNum="+str(int.from_bytes(self.navi_info["HeaderNum"],sys.byteorder))+"\n"
            sys.stderr.write(HeaderNum_Err)
            BlockLen_Err="BlockLen="+str(int.from_bytes(self.navi_info["BlockLen"],sys.byteorder))+"\n"
            sys.stderr.write(BlockLen_Err)
            return ERROR_READ
        #11-------------------------------------------------------------------
        #再次提醒！这里面有坑！一定要用正确的类型unpack！比如self.intercalib_info.gsiicsCorr_C是double类型！！！
        try:
            self.spare["HeaderNum"]=self.fp.read(1)
            self.spare["BlockLen"]=self.fp.read(2)
            self.spare["spare"]=self.fp.read(256)
        except Exception:
            sys.stderr.write("header #11 read error\n")
            return ERROR_READ
        if byteFlag==1:
            self.spare["BlockLen"]=bytes(reversed(self.spare["BlockLen"]))
        if int.from_bytes(self.spare["HeaderNum"],sys.byteorder)!=11 or int.from_bytes(self.spare["BlockLen"],sys.byteorder)!=259:
            sys.stderr.write("header #11 read error\n")
            HeaderNum_Err="HeaderNum="+str(int.from_bytes(self.navi_info["HeaderNum"],sys.byteorder))+"\n"
            sys.stderr.write(HeaderNum_Err)
            BlockLen_Err="BlockLen="+str(int.from_bytes(self.navi_info["BlockLen"],sys.byteorder))+"\n"
            sys.stderr.write(BlockLen_Err)
            return ERROR_READ
        #----------------------------------------------------------------------
        return NORMAL_END


    def radiance_to_tbb(self,radiance):
        wavelen=struct.unpack("d",self.calib_info["waveLen"])[0]
        plankConst=struct.unpack("d",self.calib_info["planckConst"])[0]
        LightSpeed=struct.unpack("d",self.calib_info["lightSpeed"])[0]
        bolzConst=struct.unpack("d",self.calib_info["bolzConst"])[0]
        rad2btp_c0=struct.unpack("d",self.calib_info["rad2btp_c0"])[0]
        rad2btp_c1=struct.unpack("d",self.calib_info["rad2btp_c1"])[0]
        rad2btp_c2=struct.unpack("d",self.calib_info["rad2btp_c2"])[0]
        #central wave length
        Lambda=wavelen/1000000.0
        #radiance
        radiance=radiance*1000000.0
        #planck_c1 = (2 * h * c^2 / lambda^5)
        planck_c1=2.0*plankConst*pow(LightSpeed,2.0)/pow(Lambda,5.0)
        #planck_c2 = (h * c / k / lambda )
        planck_c2=plankConst*LightSpeed/bolzConst/Lambda
        if radiance>0:
            effective_temperature=planck_c2/math.log((planck_c1/radiance)+1.0)
            tbb=rad2btp_c0+rad2btp_c1*effective_temperature+rad2btp_c2*pow(effective_temperature,2.0)
        else:
            tbb=INVALID_VALUE
        return tbb